package com.xiaoge.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.annotation.ControllerEndpoint;
import com.xiaoge.entity.EduCourse;
import com.xiaoge.entity.EduLecturer;
import com.xiaoge.entity.User;
import com.xiaoge.exception.BusinessException;
import com.xiaoge.mapper.EduLecturerMapper;
import com.xiaoge.response.Result;
import com.xiaoge.service.EduLecturerService;
import com.xiaoge.service.OssService;
import com.xiaoge.vo.EduCourseVO;
import com.xiaoge.vo.EduLecturerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 *@作者: 冷俊杰
 *@类名: EduLecturerController类
 *@创建时间: 2022/10/28 12:23
 *@描述:
 *讲师管理
 *@修改原图
 *@修改作者
 *@修改时间
 */
@Api(value = "讲师管理", tags = "讲师测试")
@RestController
@RequestMapping("/eduLecturer")
public class EduLecturerController {
    @Resource
    private EduLecturerService eduLecturerService;
    @Resource
    private OssService ossService;


    /**
     * 功能描述: <br>
     * 〈模糊获取讲师信息〉
     * @param current
     * @param size
     * @param lecturerName
     * @return
     */
    @ApiOperation(value = "讲师列表", notes = "模糊查询讲师")
    @PostMapping("/getEduLecturerList")
    public Result getEduLecturerList(@RequestParam(value = "current",required = true,defaultValue = "1") Integer current,
                                   @RequestParam(value = "size",required = true,defaultValue = "7") Integer size,
                                   @RequestParam(value = "lecturerName",required = true,defaultValue = "") String lecturerName){
        Page<EduLecturer> page = new Page<>(current,size);
        Page<EduLecturer> eduLecturerPage = eduLecturerService.getEduLecturerByPage(page,lecturerName);
        return Result.ok().data("eduLecturerPage",eduLecturerPage);
    }

    /**
     * 功能描述: <br>
     * 〈删除讲师〉
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除讲师失败", operation = "删除讲师")
    @RequiresPermissions({"eduLecturer:delete"})
    @ApiOperation(value = "删除讲师", notes = "删除讲师信息，根据讲师ID")
    @DeleteMapping("/delete")
    public Result delEduLecturerById(@RequestParam(value = "id",required = true) Long id){
        eduLecturerService.removeById(id);
        return Result.ok();
    }

    /**
     * 编辑讲师，获取信息
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑讲师", notes = "获取讲师的详情，编辑讲师信息")
    @RequiresPermissions({"eduLecturer:edit"})
    @GetMapping("/edit/{id}")
    public Result edit(@PathVariable Long id) {
        EduLecturer eduLecturer = eduLecturerService.getById(id);
        if(eduLecturer==null){
            throw new BusinessException("要编辑的讲师不存在");
        }
        return Result.ok().data("eduLecturer",eduLecturer);
    }

    /**
     * 修改讲师信息
     * @param eduLecturer
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改讲师信息失败", operation = "修改讲师")
    @ApiOperation(value = "修改讲师", notes = "修改讲师信息")
    @RequiresPermissions({"eduLecturer:eduLecturer"})
    @PostMapping("/eduLecturer")
    public Result eduLecturer(EduLecturerVO eduLecturer,MultipartFile file) {
        System.out.println(eduLecturer.toString());
        eduLecturer.setHeadImgUrl(ossService.upload(file));
        eduLecturerService.updateById(eduLecturer);
        return Result.ok();
    }

    /**
     * 增加讲师信息
     * @param eduLecturer
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "增加讲师信息失败", operation = "增加讲师")
    @ApiOperation(value = "增加讲师", notes = "增加讲师信息")
    @RequiresPermissions({"eduLecturer:addEduLecturer"})
    @PostMapping("/addEduLecturer")
    public Result addEduLecturer(EduLecturerVO eduLecturer,MultipartFile file) {
        eduLecturerService.addEduLecturer(eduLecturer,file);
        return Result.ok();
    }
}
